/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package customer;

import java.util.ArrayList;
import java.util.List;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;

/**
 *
 * @author jfernan
 */

@RunWith(JMock.class)
public class CustomerServiceImplTest_jmock {


         Mockery context = new JUnit4Mockery();

	/**
	 * Class underTest
	 */
	private CustomerService customerService;
	/**
	 * Mock object
	 */
	private CustomerRepository mock;

	@Before
	public void setUp() throws Exception {
		//Starting the class underTest
		this.customerService = new CustomerServiceImpl();
		//Starting mock object
		this.mock = context.mock(CustomerRepository.class);
	}
	/**
	 * This method test the getListOfPendingCustomers when none value is returned from
	 * Repository
	 * In that case, the method must return an Empty {@link java.util.List}
	 */
	@Test
	public void testGetListOfPendingCustomersWhenNoneCustomerIsRetrieved() {
		//Adding behavior for that method

        final List<Customer> listCustomers = new ArrayList<Customer>();

           context.checking(new Expectations() {{
            oneOf (mock).getListOfPendingCustomers();
            will(returnValue( listCustomers ));

        }});

        	//Injecting Repository into CustomerService
		this.customerService.setRepository(this.mock);
		//Testing the return
		assertEquals(true, this.customerService.getListOfPendingCustomers().isEmpty());
	}

	/**
	 * This method test the getListOfPendingCustomers when only ONE value is returned from the Repository
	 */
	@Test
	public void testGetListOfPendingCustomersWhenOneCustomerIsRetrieved() {
		final List<Customer> listCustomers = new ArrayList<Customer>();
		Customer customer = new Customer();
		customer.setId(1);
		customer.setName("Jair Rillo Junior");
		customer.setPhoneNumber("123456789");
		listCustomers.add(customer);


		//Adding behavior for that method


                 context.checking(new Expectations() {{
                    oneOf (mock).getListOfPendingCustomers();
                    will(returnValue( listCustomers ));

                }});


		//Injecting Repository into CustomerService
		this.customerService.setRepository(this.mock);
		//Testing the return
		List<CustomerTO> listCustomersTO = this.customerService.getListOfPendingCustomers();
		assertEquals(false, listCustomersTO.isEmpty());
		assertEquals(1, listCustomersTO.size());
		//Testing the returned value
		CustomerTO customerTO = listCustomersTO.get(0);
		assertEquals("Jair Rillo Junior", customerTO.getName());
	}
}